//
//  FoodListViewController.m
//  ArtOfLiving
//
//  Created by xalo on 16/5/1.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "FoodThemeMoreListViewController.h"
#import "FoodMainTableViewCell.h"
#import "FoodDetailViewController.h"
#import "FoodMainModel.h"
@interface FoodThemeMoreListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *parameter;       //请求数据的参数
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;       //存放请求的数据

@end

@implementation FoodThemeMoreListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    [self fetchDataSourceWithUrlString:kFoodMoreTheme parameter:self.parameter];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

#pragma mark - ------tableView的代理方法-------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceMArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FoodMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodMainCELL" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSourceMArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 170;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    FoodDetailViewController *foodDetailVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"foodDetail_CELL"];
//    [self.navigationController pushViewController:foodDetailVC animated:YES];
}

#pragma mark - ------数据请求与解析------
- (void)fetchDataSourceWithUrlString:(NSString *)urlStrinng parameter:(NSDictionary *)parameter {
    
    [LHDataRequestTools getFetchDataWithUrlString:urlStrinng paramenters:parameter success:^(id data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *dic in arr) {
            
            FoodMainModel *model = [[FoodMainModel  alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceMArr addObject:model];
        }
        [self.tableView reloadData];
    } fail:^{
        
        NSLog(@"食物更多列表解析出错");
    } view:self.view];
}

#pragma mark - -------懒加载---------
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [_tableView registerNib:[UINib nibWithNibName:@"FoodMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"foodMainCELL"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableDictionary *)parameter {
    
    if (!_parameter) {
        
        _parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"start":@0,@"num":@10}];
    }
    return _parameter;
}

- (NSMutableArray *)dataSourceMArr {
    
    if (!_dataSourceMArr) {
        
        _dataSourceMArr = [[NSMutableArray alloc] init];
    }
    return _dataSourceMArr;
}

@end
